//
//  ViewController.m
//  UI10_homework通讯录UI
//
//  Created by dllo on 16/6/17.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "ViewController.h"
#import "SecondViewController.h"


@interface ViewController ()<UITableViewDataSource, UITableViewDelegate, SecondViewController>


@property(nonatomic, retain)NSMutableDictionary *wordsDictionary;
@property(nonatomic, retain)NSMutableArray *keysArray;

@property(nonatomic, assign)NSInteger  rowFlag;
@property(nonatomic, assign)NSInteger  sectionFlag;
@property(nonatomic , strong) UITableView *contactTableView;

@end

@implementation ViewController


- (void)dealloc
{
    [_keysArray release];
    [_wordsDictionary release];
    [super dealloc];
}



- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    // 初始化
    self.wordsDictionary = [[NSMutableDictionary alloc]init];
    self.keysArray = [[NSMutableArray alloc]init];
    
    
    [self handleData];
    [self tabBarView];
    [self creatTableView];
    self.automaticallyAdjustsScrollViewInsets = NO;
}


// 设置第一页导航栏
-(void)tabBarView{
    
    self.navigationItem.title = @"All Contacts";
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(add:)];
    
    
}
-(void)add:(UITabBarItem *)tabBar{
    
}



// 创建UItableView视图
-(void)creatTableView{
    
    _contactTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds) - 64) style:UITableViewStylePlain];
    
    [self.view addSubview:_contactTableView];
    
    // 行高
    _contactTableView.rowHeight = 80;
    _contactTableView.separatorColor = [UIColor grayColor];
    
    
    
    _contactTableView.delegate = self;
    _contactTableView.dataSource = self;
    
    // 创建重用池
    [_contactTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    
    
    [_contactTableView release];
    
    
}


// 返回分区数
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    // 字母数
    return self.keysArray.count;
}

// 返回每区的行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
   
//    NSArray *array = [self.wordsDictionary valueForKey:self.keysArray(section)];
    
    
    return [NSArray arrayWithArray:[self.wordsDictionary valueForKey:[self.keysArray objectAtIndex:section]]].count;
    
}


// 设置每行的内容
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *tableViewCell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    
    NSArray *array = [self.wordsDictionary valueForKey:[self.keysArray objectAtIndex:indexPath.section]];

    NSDictionary *namedictionary =[array objectAtIndex:indexPath.row];
    
    
    
//    UITableViewCell *tableViewCell = [[UITableViewCell alloc]initWithFrame:CGRectMake(0, 0, 260, 210)];
//    tableViewCell.textLabel.text = @"textLabel.text";
    tableViewCell.textLabel.text = [namedictionary objectForKey:@"name"];
    
    
    tableViewCell.imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@",[namedictionary objectForKey:@"header"]]];
//    NSLog(@"%@", [namedictionary objectForKey:@"header"]);
//    tableViewCell.imageView.image = [UIImage imageNamed:@"aisi.jpg"];
    
    tableViewCell.imageView.layer.cornerRadius = 40;
    
    tableViewCell.imageView.clipsToBounds = YES;
    
    tableViewCell.detailTextLabel.text = @"detailTextLabel.text";
   
    // 系统样式
    tableViewCell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    return tableViewCell;
    
    
}

// 侧边栏
-(NSArray <NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView{
    return self.keysArray;
    
}





// 设置分区头标题
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
//    return @"titleForHeader";
    
    return [NSString stringWithFormat:@"%@", [self.keysArray objectAtIndex:section]];
}


//// 设置分区尾标题
//-(NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{
//    return @"titleForFooter";
//}




//// 分区头高度
//-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
//    return 40.0;
//}


// 数据处理
-(void)handleData{
    
    // 从系统中获取数据
    NSString *path = [[NSBundle mainBundle]pathForResource:@"AllContacts" ofType:@".plist"];
    self.wordsDictionary = [[NSMutableDictionary alloc]initWithContentsOfFile:path];
    NSLog(@"%@", self.wordsDictionary);
    
    self.keysArray = [NSMutableArray arrayWithArray:[self.wordsDictionary allKeys]];
    
    
}


// 第一页面到第二页面的接口

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    SecondViewController *secondVC = [[SecondViewController alloc]init];
   
    NSArray *array = [self.wordsDictionary valueForKey:[self.keysArray objectAtIndex:indexPath.section]];
    secondVC.name = [[array objectAtIndex:indexPath.row] valueForKey:@"name"];
//    NSLog(@"%@", secondVC.name);
    secondVC.headerImage = [UIImage imageNamed:[NSString stringWithFormat:@"%@", [[array objectAtIndex:indexPath.row] valueForKey:@"header"]]];
    secondVC.number = [[array objectAtIndex:indexPath.row] valueForKey:@"电话"];
    secondVC.qq = [[array objectAtIndex:indexPath.row] valueForKey:@"QQ"];
    
//    指定代理人
    secondVC.delegate = self;
    
    
    // 进行一下标记
    self.rowFlag = indexPath.row;
    self.sectionFlag = indexPath.section;
    
    
    [self.navigationController pushViewController:secondVC animated:YES];
    
}


#pragma mark - 方法你妹的代理的
-(void)sentNameText:(NSString *)text withNumber:(NSString *)numbertext withQQ:(NSString *)qqNumber withNewImage:(UIImage *)newImage{
    
    NSLog(@"%@,%@,%@", text, numbertext, qqNumber);
    
//    self.wordsDictionary =
    
    NSMutableArray *newArray = [self.wordsDictionary valueForKey:[self.keysArray objectAtIndex:self.sectionFlag]];
    NSMutableDictionary *    newDictionary = [NSMutableDictionary dictionaryWithDictionary:[newArray objectAtIndex:self.rowFlag]];
    
//    NSMutableDictionary *newDictionary = [NSMutableDictionary dictionary];
//    [newArray removeObject: [NSMutableDictionary dictionaryWithDictionary:[newArray objectAtIndex:self.rowFlag]]];
    [newArray removeObjectAtIndex:self.rowFlag];
    
    [newDictionary setValue:text forKey:@"name"];
    [newDictionary setValue:numbertext forKey:@"电话"];
    [newDictionary setValue:qqNumber forKey:@"QQ"];
    [newDictionary setValue:newImage forKey:@"header"];
    
    NSLog(@"%@", newDictionary.allValues);
    [newArray addObject:newDictionary];
    
    [self.wordsDictionary setValue:newArray forKey:[self.keysArray objectAtIndex:self.sectionFlag]];
    NSLog(@"%@", self.wordsDictionary);
    
    
    
    [_contactTableView reloadData];
    
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
